package com.gobo.ucenterservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gobo.commonutils.JwtUtils;
import com.gobo.commonutils.MD5;
import com.gobo.servicebase.handler.exception.pojo.GoboException;
import com.gobo.servicebase.vo.MemberInfoVo;
import com.gobo.ucenterservice.pojo.UcenterMember;
import com.gobo.ucenterservice.mapper.UcenterMemberMapper;
import com.gobo.ucenterservice.pojo.vo.LoginInfoVo;
import com.gobo.ucenterservice.pojo.vo.RegisterMember;
import com.gobo.ucenterservice.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author gobo-generator
 * @since 2021-09-23
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 添加用户
     * @param registerMember
     * @return
     */
    @Override
    public Boolean saveRegisterMember(RegisterMember registerMember) {
        //判断验证码是否正确
        if (!registerMember.getCode().equals(redisTemplate.opsForValue().get("register"+registerMember.getMobile()))){
            return false;
        }
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",registerMember.getMobile());
        Integer count = baseMapper.selectCount(queryWrapper);
        if (count>0){
            return false;
        }
        String md5Password = MD5.encrypt(registerMember.getPassword());
        UcenterMember member = new UcenterMember();
        BeanUtils.copyProperties(registerMember,member);
        member.setIsDisabled(false);
        member.setAvatar("https://gobo-edu-online.oss-cn-qingdao.aliyuncs.com/default/avatar.jpg");
        member.setPassword(md5Password);
        System.out.println(member);
        int result = baseMapper.insert(member);
        return result==1;
    }

    @Override
    public String checkMember(UcenterMember ucenterMember) {
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",ucenterMember.getMobile());
        queryWrapper.eq("password",MD5.encrypt(ucenterMember.getPassword()));
        UcenterMember member = baseMapper.selectOne(queryWrapper);
        if (member==null){
            return null;
        }
        if(member.getIsDisabled()) {
            throw new GoboException(30001,"账户已被禁用");
        }
        String token = JwtUtils.getJwtToken(member.getId(),member.getNickname());

        return token;
    }

    @Override
    public LoginInfoVo getLoginInfo(String memberId) {

        if (memberId==null){
            throw  new GoboException(30001,"token已失效");
        }
        UcenterMember member = baseMapper.selectById(memberId);
        LoginInfoVo loginInfoVo = new LoginInfoVo();
        BeanUtils.copyProperties(member,loginInfoVo);
        return loginInfoVo;
    }

    @Override
    public UcenterMember getWxMemberInfo(String openid) {

        QueryWrapper<UcenterMember> queryWrapper  = new QueryWrapper<>();
        queryWrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(queryWrapper);
        if (member!=null){
            if (member.getIsDisabled()){
                throw  new GoboException(30001,"用户已被禁用！");
            }
        }
        return member;
    }

    @Override
    public MemberInfoVo getMemberInfoVo(String id) {
        UcenterMember member = baseMapper.selectById(id);
        if (member!=null){
            if (member.getIsDisabled()){
                throw  new GoboException(30001,"用户已被禁用！");
            }
        }
        MemberInfoVo memberInfoVo = new MemberInfoVo();
        BeanUtils.copyProperties(member,memberInfoVo);
        return memberInfoVo;
    }

    @Override
    public Integer countRegisterByDay(String day) {
        Integer registerCount = baseMapper.selectRegisterCount(day);
        return registerCount;
    }
}
